package Two;

import java.util.List;

public class BuyImpl implements IBuy{
    private List<Commodity> list = FileControl.getList();
    private List<Commodity> l = BuyListFileControl.getBuyList();

    @Override
    public List<Commodity> showAll() {
        return list;
    }

    @Override
    public Commodity search(int code) {
        int index = list.indexOf(new Commodity(code));
        if (index!=-1){
            return list.get(index);
        }
        return null;
    }

    @Override
    public void add(Commodity c) {
        l.add(c);
        BuyListFileControl.writer();
    }

    @Override
    public List<Commodity> showBuy() {
        return l;
    }

    @Override
    public void updateNumber(int code) {

        int x = l.indexOf(new Commodity(code));
        if (x!=-1){
            Commodity commodity = l.get(x);
            int newNum = l.get(x).getNumber()+1;
            commodity.setNumber(newNum);

            BuyListFileControl.writer();
        }else {
            int i = list.indexOf(new Commodity(code));
            Commodity commodity = new Commodity(list.get(i).getCode(),list.get(i).getName(),list.get(i).getPrice(),1);
            this.add(commodity);
        }




    }


}
